Logical Operators

In javascript, the logical operators returns true or false based on the expression.

Operator Operator Name
&& AND operator
|| Logical OR
! Logical NOT

AND Operator

The AND operator is represented with two ampersands &&. It returns true if all the expressions that are concatenated with && satisfies the condition, else it will return false.

Example 1

console.log( true && true );   // true
console.log( 5>8 && 10 > 9);  // false
console.log(10 >8 && 10 > 9); // true

Example 2

let hour = 12;
if(hour > 9 && hour < 20){
 console.log('Shop is open'); 
} 

OR Operator

The OR operator is denoted by two vertical lines ||. It returns true if any one of the expressions that are concatenated with || satisfies the condition, else it will return false.

Example 1

console.log( true || true );   // true
console.log( 5>8 || 10 > 9);  // true
console.log(10 >8 || 10 > 9); // true

Example 2

if(5 >8 || 10 > 9){
    console.log('true'); 
}

NOT Operator

The logical NOT operator is denoted with the exclamation symbol !(NOT). This reverses or negates the value of a boolean.

Example 1

console.log(!false); // true
console.log(!1); // false

Example 2

let age = 18;
if(!age<18){
    console.log('You are free');
}

Most Read